package yue.shen.android.demo.adapter;

import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;

import java.util.ArrayList;
import java.util.List;

import yue.shen.android.demo.R;

public class RViewActivity extends BaseRViewActivity<RViewData> {

    private Context context;
    private RViewAdapter<RViewData> adapter;
    private List<RViewData> list;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        context = this;
        list = new ArrayList<>();
        initListener();
        initData();
    }

    private void initListener() {
        adapter.setItemListener(new ItemListener<RViewData>() {
            @Override
            public void onItemClick(View view, RViewData entity, int position) {
                Toast.makeText(context, "onItemClick:" + position, Toast.LENGTH_LONG).show();
            }

            @Override
            public boolean onItemLongClick(View view, RViewData entity, int position) {
                Toast.makeText(context, "onItemLongClick:" + position, Toast.LENGTH_LONG).show();
                return true;
            }
        });
    }

    private void initData() {
        if (list.isEmpty()) {
            for (int i = 0; i < 100; i++) {
                list.add(new RViewData());
            }
        } else {
            int size = list.size() - 1;
            for (int i = size; i < size + 100; i++) {
                list.add(new RViewData());
            }
        }
        notifyAdapterDataSetChanged(list);
    }

    @Override
    public RViewAdapter<RViewData> createRecycleViewAdapter() {
        adapter = new RViewAdapter<>(list, new RViewItem<RViewData>() {

            @Override
            public int getItemLayout() {
                return R.layout.r_view_recycle_view_item;
            }

            @Override
            public boolean openClick() {
                return true;
            }

            @Override
            public boolean isItemView(RViewData entity, int position) {
                return true;
            }

            @Override
            public void convert(RViewHolder holder, RViewData entity, int position) {
                TextView textView = holder.getView(R.id.text_view);
                textView.setText("position" + position);
            }
        });
        return adapter;
    }

    @Override
    public void onRefresh() {
        initData();
    }
}
